package com.wang.study.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wang.study.commons.MybatisUtils;
import com.wang.study.mapper.GoodsMapper;
import com.wang.study.model.Goods;
import com.wang.study.service.GoodsService;

import java.util.List;

public class GoodsServiceImp implements GoodsService {

    private GoodsMapper goodsMapper;

    public GoodsServiceImp(){
        goodsMapper = MybatisUtils.getMapper(GoodsMapper.class);
    }

    /**
     * 查询商品的操作
     * @param cid
     * @return
     */
    public PageInfo<Goods> findGoodsByCid(Integer cid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //通过大类cid来查询改类的所有商品
        List<Goods> goods = goodsMapper.findGoodsByCid(cid);
        //通过分页插件来返回查询到的商品
        return new PageInfo<>(goods);
    }

    @Override
    public Goods selectGoodById(String cid) {
        Goods good=goodsMapper.selectGoodById(cid);
        return good;
    }

    @Override
    public PageInfo<Goods> findGoodsAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //通过大类cid来查询改类的所有商品
        List<Goods> goods = goodsMapper.findGoodsAll();
        //通过分页插件来返回查询到的商品
        return new PageInfo<>(goods);
    }

    @Override
    public PageInfo<Goods> findGoodsByPageNum(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //通过大类cid来查询改类的所有商品
        List<Goods> goods = goodsMapper.findGoodsAll();
        //通过分页插件来返回查询到的商品
        return new PageInfo<>(goods);
    }

    @Override
    public List<Goods> findGoodsAll1() {
        List<Goods> goods = goodsMapper.findGoodsAll();
        return goods;
    }

    @Override
    public Integer delGoodsById(Integer id) {
        return goodsMapper.delGoodsById(id);
    }

    @Override
    public Integer addGoods(Integer id,String author, String publisher, String publishtime, String decription, String image, String salesprice, String status, String uptime) {
        return goodsMapper.addGoods(id,author,publisher,publishtime,decription,image,salesprice,status,uptime);
    }

    @Override
    public Integer updateGoodById(Integer id,String imagePath) {
        //修改数据库路径
        Integer i = goodsMapper.updateGoodById(id, imagePath);
        return i;
    }

    @Override
    public List<Goods> likeSearch(String name) {
        List<Goods> goods = goodsMapper.likeSearch(name);
        return goods;
    }
}
